var site_url=localStorage.getItem("site_url");

var requestStatus=false;
$(function () {
	/* var userInfo = getUserInfo();
	var dept = getDeptById(userInfo.deptId);
	$("#search_project").html($('<option value="-1">全部</option>'));
	$("#search_department").html($('<option value="">全部</option>'));
	for(var i = 0;i<dept.length;i++){
		var gaoji,querynames = "";
		if (dept[i].type == 1) {
			gaoji = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
		}else{
			if(userInfo.deptId==dept[i].id){
				querynames = $('<option value="'+dept[i].id+'" selected="selected">'+dept[i].name+'</option>');
			}else{
				querynames = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
			}
		}
		
		$("#search_project").append(gaoji);
		$("#search_department").append(querynames);
	} */
});
function getParam(){
	//当前页数
	var pageNo=$('#pageNo').val();
	//总页数
	var total_page=$('#total_page').val();
	//总条数
	var total_rows=$('#total_rows').val();
	var keyword=$('#keyword').val();
	
	var deptId=$("#search_department option:selected").val(); 
	var start=$('#search_date_start').val();
	
	var end=$('#search_date_end').val();
	
	var keyword=$('#search_keyword').val();
	var userDepartId=localStorage.getItem("departId"); 
	var param = {
		current:pageNo,
	 	deptId:deptId==undefined?userDepartId:deptId,
	 	start:start,
	 	end:end,
	 	keyword:keyword
	};
	return param;    
}
/* 财务管理的开始函数 */
function pageStart(id){

	//当前页数
    var pageNo=$('#pageNo').val();
    //总页数
    var total_page=$('#total_page').val();
    //总条数
    var total_rows=$('#total_rows').val();
    var keyword=$('#keyword').val();
    
    var deptId=$("#search_department option:selected").val(); 
    
    var start=$('#search_date_start').val();
    
    var end=$('#search_date_end').val();
    
    var keyword=$('#search_keyword').val();
    var userInfo=JSON.parse(localStorage.getItem("userInfo"));
    var userDepartId=userInfo.deptId; 
	
	 var param = {
	 	current:pageNo,
	  	deptId:deptId==undefined?userDepartId:deptId,
	  	start:start,
	  	end:end,
	  	keyword:keyword
	 };
	// getData();
	//分页
	$("#search_project").html($('<option value="-1">全部</option>'));
	$("#search_department").html($('<option value="">全部</option>'));
	// getProject();
	switch(id){
		//待审批页面(加班申请)
		case 311:
			var userInfo = getUserInfo();
			var dept = getDeptById(userInfo.deptId);
			for(var i = 0;i<dept.length;i++){
				var gaoji,querynames = "";
				if (dept[i].type == 2) {
					gaoji = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
				}
				
				if (dept[i].type == 3){
					if(userInfo.deptId==dept[i].id){
						querynames = $('<option value="'+dept[i].id+'" selected="selected">'+dept[i].name+'</option>');
					}else{
						querynames = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
					}
				}
				
				$("#search_project").append(gaoji);
				$("#search_department").append(querynames);
			}
			getDataOvertime(param);
			break;	
		//待审批页面(补卡说明)
		case 312:
			var userInfo = getUserInfo();
			var dept = getDeptById(userInfo.deptId);
			
			for(var i = 0;i<dept.length;i++){
				var gaoji,querynames = "";
				if (dept[i].type == 2) {
					gaoji = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
				}
				
				if (dept[i].type == 3){
					if(userInfo.deptId==dept[i].id){
						querynames = $('<option value="'+dept[i].id+'" selected="selected">'+dept[i].name+'</option>');
					}else{
						querynames = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
					}
				}
				
				$("#search_project").append(gaoji);
				$("#search_department").append(querynames);
			}
			getDataPatchcard(param);
			break;
		//待审批页面(请假申请)
		case 313:
			var userInfo = getUserInfo();
			var dept = getDeptById(userInfo.deptId);
			
			for(var i = 0;i<dept.length;i++){
				var gaoji,querynames = "";
				if (dept[i].type == 2) {
					gaoji = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
				}
				
				if (dept[i].type == 3){
					if(userInfo.deptId==dept[i].id){
						querynames = $('<option value="'+dept[i].id+'" selected="selected">'+dept[i].name+'</option>');
					}else{
						querynames = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
					}
				}
				
				$("#search_project").append(gaoji);
				$("#search_department").append(querynames);
			}
			getDataLeave(param);
			break;
		//考勤记录页面
		case 32:
			var time = new Date();
	        var day = ("0" + time.getDate()).slice(-2);
	        var month = "";
			var monthNum=time.getMonth() + 1;
			//小于10月，前面加0
			if(monthNum<10){
				month = ("0" + (time.getMonth() + 1)).slice(-2);
			}
	        var today = time.getFullYear() + "-" + (month);
			$('#search_month').val(today);
			// var search_month=$('#search_month').val();
			
			var param = {
				current:$('#pageNo').val(),
			 	deptId:deptId==(undefined || "")?userDepartId:deptId,
			 	start:today,
			 	end:"",
			 	keyword:keyword
			};
			var userInfo = getUserInfo();
			var dept = getDeptById(userInfo.deptId);
			
			for(var i = 0;i<dept.length;i++){
				var gaoji,querynames = "";
				if (dept[i].type == 2) {
					gaoji = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
				}
				
				if (dept[i].type == 3){
					if(userInfo.deptId==dept[i].id){
						querynames = $('<option value="'+dept[i].id+'" selected="selected">'+dept[i].name+'</option>');
					}else{
						querynames = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
					}
				}				
				$("#search_project").append(gaoji);
				$("#search_department").append(querynames);
			}
			getDataRecord(param);
			break;
		//考勤记录详情页面
		case 321:
			var date=localStorage.getItem("attendanceDate");
			var userId=localStorage.getItem("attendanceUserId");
			var param = {
				current:pageNo,
				userId:userId,
				start:date,
				end:end,
			};
			getDataRecordInfo(param);
			break;	
		//审批记录页面(加班申请)
		case 331:
			var userInfo = getUserInfo();
			var dept = getDeptById(userInfo.deptId);
			
			for(var i = 0;i<dept.length;i++){
				var gaoji,querynames = "";
				if (dept[i].type == 2) {
					gaoji = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
				}
				
				if (dept[i].type == 3){
					if(userInfo.deptId==dept[i].id){
						querynames = $('<option value="'+dept[i].id+'" selected="selected">'+dept[i].name+'</option>');
					}else{
						querynames = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
					}
				}
				
				$("#search_project").append(gaoji);
				$("#search_department").append(querynames);
			}
			getDataOvertimeApproval(param);
			break;	
		//审批记录页面(补卡说明)
		case 332:
			var userInfo = getUserInfo();
			var dept = getDeptById(userInfo.deptId);
			
			for(var i = 0;i<dept.length;i++){
				var gaoji,querynames = "";
				if (dept[i].type == 2) {
					gaoji = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
				}
				
				if (dept[i].type == 3){
					if(userInfo.deptId==dept[i].id){
						querynames = $('<option value="'+dept[i].id+'" selected="selected">'+dept[i].name+'</option>');
					}else{
						querynames = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
					}
				}
				
				$("#search_project").append(gaoji);
				$("#search_department").append(querynames);
			}
			getDataPatchcardApproval(param);
			break;
		//审批记录页面(请假申请)
		case 333:
			var userInfo = getUserInfo();
			var dept = getDeptById(userInfo.deptId);
			
			for(var i = 0;i<dept.length;i++){
				var gaoji,querynames = "";
				if (dept[i].type == 2) {
					gaoji = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
				}
				
				if (dept[i].type == 3){
					if(userInfo.deptId==dept[i].id){
						querynames = $('<option value="'+dept[i].id+'" selected="selected">'+dept[i].name+'</option>');
					}else{
						querynames = $('<option value="'+dept[i].id+'">'+dept[i].name+'</option>');
					}
				}
				
				$("#search_project").append(gaoji);
				$("#search_department").append(querynames);
			}
			getDataLeaveApproval(param);	   
			break;
		default:
			break;
	}	
	
}
/* 项目部赋值 */
//查询部门
function getProject(){
	var URL = site_url+"/depts/all";
	var userToken=localStorage.getItem("userToken");
	var userInfo=JSON.parse(localStorage.getItem("userInfo"));
	var userDepartId=userInfo.deptId;
	$.ajax({
		type:"get",
		url:URL,
		headers:{'token':userToken},
		success:function(data){
			var querynames = "";
			$.each(data, function(index, dept) {
				
				if(userDepartId==dept.id){
					querynames += '<option value="'+dept.id+'" selected="selected">'+dept.name+'</option>';
				}else{
					querynames += '<option value="'+dept.id+'">'+dept.name+'</option>';
				}
				
			});
			$("#search_department").append(querynames);
		}
	});
}
//给考勤记录页面赋值
function getDataRecord(param){
	
	if(param.deptId==""){
		param.deptId=-1;
	}
	// alert(param.deptId);
	var userToken=localStorage.getItem("userToken");
	var URL = site_url+"/clockins/attendance/record?size=10&current="+param.current+"&deptId="+param.deptId+"&start="+param.start+"&end="+param.end+"&keyword="+param.keyword;
	
	$.ajax({
		type:"get",
		url:URL,
		headers:{'token':userToken},
		success:function(data){
			var result = data.data.records;
			// alert(result.length);
			$('#data-record').html("");
			var html="";
			html+='<table style="table-layout: auto;">';
			html+='<tr class="table-top">';
			//html+='<td width="3%"></td><td width="5%">序号</td><td width="7%">部门</td><td width="7%">姓名</td><td width="5%">公休</td>';
			html+='<td width="3%"></td><td width="5%">序号</td><td width="10%">部门</td><td width="9%">姓名</td>';
			html+='<td width="5%">事假</td><td width="5%">病假</td><td width="5%">婚假</td><td width="5%">丧假</td><td width="5%">产假</td>';
			html+='<td width="5%">加班</td><td width="5%">调休</td><td width="5%">迟到</td><td width="5%">旷工</td><td width="5%">早退</td>';
			html+='<td width="6%">可调休</br>时间</td><td width="6%">应出勤</br>天数</td><td width="6%">实际出勤</br>天数</td><td width="5%">操作</td></tr>';
			
			for (var i=0;i<result.length;i++) {
				if(i%2==0){
					html+='<tr class="item-white">';
				}else{
					html+='<tr class="item-blue">';
				}
				html+='<td></td>';
				html+='<td>'+(i+1)+'</td>';	
				html+='<td>'+result[i].deptName+'</td>';
				html+='<td>'+result[i].userName+'</td>';
				/* var shijia =result[i].shijia==null?"0":result[i].shijia;
				html+='<td>'+shijia+'</td>'; */
				var shijia =result[i].shijia==null?"0":result[i].shijia;
				html+='<td>'+shijia+'</td>';
				var bingjia =result[i].bingjia==null?"0":result[i].bingjia;
				html+='<td>'+bingjia+'</td>';
				var hunjia =result[i].hunjia==null?"0":result[i].hunjia;
				html+='<td>'+hunjia+'</td>';
				var sangjia =result[i].sangjia==null?"0":result[i].sangjia;
				html+='<td>'+sangjia+'</td>';
				var chanjia =result[i].chanjia==null?"0":result[i].chanjia;
				html+='<td>'+chanjia+'</td>';
				var jiaban =result[i].jiaban==null?"0":result[i].jiaban;
				html+='<td>'+jiaban+'</td>';
				var tiaoxiu =result[i].tiaoxiu==null?"0":result[i].tiaoxiu;
				html+='<td>'+tiaoxiu+'</td>';
				var chidao =result[i].chidao==null?"0":result[i].chidao;
				html+='<td>'+chidao+'</td>';
				var kuanggong =result[i].kuanggong==null?"0":result[i].kuanggong;
				html+='<td>'+kuanggong+'</td>';
				var zaotui =result[i].zaotui==null?"0":result[i].zaotui;
				html+='<td>'+zaotui+'</td>';
				var ketiao =result[i].ketiao==null?"0":result[i].ketiao;
				html+='<td>'+ketiao+'</td>';
				var yingqin =result[i].yingqin==null?"0":result[i].yingqin;
				html+='<td>'+yingqin+'</td>';
				var shiqin =result[i].shiqin==null?"0":result[i].shiqin;
				html+='<td>'+shiqin+'</td>';
			
				html+='<td>';
				html+='<img id="info" style="margin-left: 10px;" src="img/check_icon.png" onclick="dataInfo(4,\''+result[i].userId+'\');"/>';
				html+='</td>';
				html+='</tr>';
			}
			if(result.length<=0){
				html+='<tr class="item-white">';
				html+='<td colspan="19"><div>暂无数据</div></td>';
				html+='</tr>';
				$("#page").html("");
			}else{
				if(!requestStatus){
					/* var pageNo=data.data.current;
					var total_page=data.data.pages;
					//分页
					$("#page").paging({
					    pageNo: parseInt(pageNo),
					    totalPage: parseInt(total_page),
					    callback: function(num) {
							$('#pageNo').val(num);
							requestStatus=true;
							var param=getParam();
							var time = new Date();
					        var day = ("0" + time.getDate()).slice(-2);
					        var month = "";
							var monthNum=time.getMonth() + 1;
							//小于10月，前面加0
							if(monthNum<10){
								month = ("0" + (time.getMonth() + 1)).slice(-2);
							}
					        var today = time.getFullYear() + "-" + (month);
							$('#search_month').val(today);
							param.start=today;
							// param.current=num;
							getDataRecord(param);
					    }
					}) */
				}
				var pageNo=data.data.current;
				var total_page=data.data.pages;
				localStorage.setItem("total_page",total_page);
				//分页
				$("#page").paging({
				    pageNo: parseInt(pageNo),
				    totalPage: parseInt(total_page),
				    callback: function(num) {
						$('#pageNo').val(num);
						var total_page=localStorage.getItem("total_page");
						$("#page").paging({
						    pageNo: parseInt(num),
						    totalPage: parseInt(total_page)
						})
						var paramData=getParam();
						var today=getDate();
						// $('#search_month').val(today);
						paramData.start=$('#search_month').val();     
						getDataRecord(paramData);
						$("#page").stopPagination({});
				    }
				})
			}
			html+='</table>';
			$('#data-record').html(html);

			
		}
	});
}
function getDate(){
	var time = new Date();
	var day = ("0" + time.getDate()).slice(-2);
	var month = "";
	var monthNum=time.getMonth() + 1;
	//小于10月，前面加0
	if(monthNum<10){
		month = ("0" + (time.getMonth() + 1)).slice(-2);
	}
	var today = time.getFullYear() + "-" + (month);
	return today;
}
//给考勤记录详情页面赋值
function getDataRecordInfo(param){
	var userToken=localStorage.getItem("userToken");
	var URL = site_url+"/clockins/attendance/record/page?size=10&current="+param.current+"&userId="+param.userId+"&start="+param.start+"&end="+param.end;
	
	$.ajax({
		type:"get",
		url:URL,
		headers:{'token':userToken},
		success:function(data){
			var result = data.data.records;
			$('#data-record-info').html("");
			var html="";
			html+='<table style="table-layout: auto;">';
			html+='<tr class="table-top">';
			html+='<td width="10%">序号</td><td width="15%">姓名</td><td width="15%">部门</td><td width="15%">日期</td>';
			html+='<td width="15%">签到时间</td><td width="15%">签退时间</td><td width="15%">考勤状态</td></tr>';
			
			for (var i=0;i<result.length;i++) {
				if(i%2==0){
					html+='<tr class="item-white">';
				}else{
					html+='<tr class="item-blue">';
				}
				html+='<td>'+(i+1)+'</td>';	
				html+='<td>'+result[i].userName+'</td>';
				html+='<td>'+result[i].deptName+'</td>';
				html+='<td>'+result[i].workDay+'</td>';
				
				var workShift="";
				if(result[i].workShift!=null){
					workShift=(result[i].workShift).substring(10,20);
				}
				var closingTime="";
				if(result[i].closingTime!=null){
					closingTime=(result[i].closingTime).substring(10,20);
				}
				html+='<td>'+workShift+'</td>';
				html+='<td>'+closingTime+'</td>';
				var status=result[i].clockInStatus;
				// alert(status);
				if(status==1){
					html+='<td>迟到</td>';
				}else if(status==2){
					html+='<td>请假</td>';
				}else if(status==3){
					html+='<td>上午缺卡</td>';
				}else if(status==4){
					html+='<td>早退</td>';
				}else if(status==5){
					html+='<td>旷工</td>';
				}else if(status==6){
					html+='<td>迟到早退</td>';
				}else if(status==7){
					html+='<td>外勤</td>';
				}else if(status==8){
					html+='<td>休息</td>';
				}else if(status==9){
					html+='<td>下午缺卡</td>';
				}else{
					html+='<td>正常</td>';
				}
			}
			if(result.length<=0){
				html+='<tr class="item-white">';
				html+='<td colspan="18"><div>暂无数据</div></td>';
				html+='</tr>';
			}else{
				if(!requestStatus){
					var pageNo=data.data.current;
					var total_page=data.data.pages;
					//分页
					$("#page").paging({
					    pageNo: parseInt(pageNo),
					    totalPage: parseInt(total_page),
					    callback: function(num) {
							$('#pageNo').val(num);
							requestStatus=true;
							var param=getParam();
							// param.current=num;
							getDataRecordInfo(param);
					    }
					}) 
				}
			}
			html+='</table>';
			$('#data-record-info').html(html);
		}
	});

}
/* -------------------------未审批数据赋值开始------------------------- */
/* 给待审批（加班申请）页面赋值 */
function getDataOvertime(param){
	
    var userToken=localStorage.getItem("userToken");
	var URL = site_url+"/overtimes/page?size=10&current="+param.current+"&deptId="+param.deptId+"&start="+param.start+"&end="+param.end+"&keyword="+param.keyword+"&result=11"
	// var URL = site_url+"/overtimes/approve"
	
	$.ajax({
		type:"get",
		url:URL,
		headers:{'token':userToken},
		success:function(data){
			var result = data.data.records;
			  // console.log(result);
			  // alert(result.length);

			$('#pageNo').val(data.data.current);
			var total_page=data.data.total/9;
			$('#data_overtime').html("");
			var html="";
			html+='<table>';
			html+='<tr class="table-top"><td width="6%"><input id="allCheck" type="checkbox" value="0" onchange="selectAll()"></td>';
			html+='<td width="8%">姓名</td><td width="8%">部门</td><td width="8%">加班类型</td><td width="16%">开始时间</td>';
			html+='<td width="16%">结束时间</td><td width="8%">加班时长</td><td width="17%">加班内容</td><td width="15%">操作</td></tr>';
		
			for (var i=0;i<result.length;i++) {
				var end=result[i].end;
				var duration=result[i].duration;
				var status=0;
				//传值12
				if(end=="" || duration==""){
					status=12;
				}
				//传值1
				else if(end!="" && duration!=""){
					status=1;
				}
				// alert(status);
				if(i%2==0){
					html+='<tr class="item-white" onclick="dataCheck(1,\''+result[i].id+'\',\''+status+'\');">';
				}else{
					html+='<tr class="item-blue" onclick="dataCheck(1,\''+result[i].id+'\',\''+status+'\');">';
				}
				html+='<td><input type="checkbox" name="dataId" value="'+result[i].id+'"></td>';
				html+='<td>'+(result[i].name==null?"":result[i].name)+'</td>';
				html+='<td>'+(result[i].deptName==null?"":result[i].deptName)+'</td>';
				var type=result[i].type==1?"调休":"有偿";
				html+='<td>'+type+'</td>';
				html+='<td>'+(result[i].start==null?"":result[i].start)+'</td>';
				html+='<td>'+(result[i].end==null?"":result[i].end)+'</td>';
				html+='<td>'+(result[i].duration==null?"":result[i].duration)+'</td>';
				html+='<td title="'+result[i].reason+'">'+(result[i].reason==null?"":result[i].reason)+'</td>';
				html+='<td>';
				
				html+='<button class="passBtn" onclick="pass(1,\''+result[i].id+'\',\''+status+'\');">通过</button>';
				html+='<button class="rejectBtn" onclick="rejectShow(1,\''+result[i].id+'\');">驳回</button>';
				html+='</td>';
				html+='</tr>';
			}
			if(result.length<=0){
				html+='<tr class="item-white">';
				html+='<td colspan="9"><div>暂无数据</div></td>';
				html+='</tr>';
				$("#page").html("");
			}else{
				var pageNo=data.data.current;
				var total_page=data.data.pages;
				localStorage.setItem("total_page",total_page);
				//分页
				$("#page").paging({
				    pageNo: parseInt(pageNo),
				    totalPage: parseInt(total_page),
				    callback: function(num) {
						$('#pageNo').val(num);
						var total_page=localStorage.getItem("total_page");
						$("#page").paging({
						    pageNo: parseInt(num),
						    totalPage: parseInt(total_page)
						})
						var paramData=getParam();
						getDataOvertime(paramData);
						$("#page").stopPagination({});
				    }
				})
			}
			html+='</table>';
			$('#data_overtime').html(html);	
			
		}
	});
	
}
/* 给待审批（补卡说明）页面赋值 */
function getDataPatchcard(param){
	
	var userToken=localStorage.getItem("userToken");
	var URL = site_url+"/replacecards/page?size=10&current="+param.current+"&deptId="+param.deptId+"&start="+param.start+"&end="+param.end+"&keyword="+param.keyword+"&result=0";
	// var URL = site_url+"/replacecards/approve";

	$.ajax({
		type:"get",
		url:URL,
		headers:{'token':userToken},
		success:function(data){
			var result = data.data.records;
			$('#data_patchcard').html("");
			var html="";
			html+='<table>';
			html+='<tr class="table-top"><td width="10%"><input id="allCheck" type="checkbox" value="0" onchange="selectAll()"></td><td width="10%">姓名</td>';
			html+='<td width="15%">部门</td><td width="20%">补卡时间</td><td width="25%">补卡原因</td><td width="20%">操作</td>';
		
			for (var i=0;i<result.length;i++) {
				if(i%2==0){
					html+='<tr class="item-white" onclick="dataCheck(2,\''+result[i].id+'\',0);">';
				}else{
					html+='<tr class="item-blue" onclick="dataCheck(2,\''+result[i].id+'\',0);">';
				}
				html+='<td><input type="checkbox" name="dataId" value="'+result[i].id+'"></td>';
				html+='<td>'+result[i].name+'</td>';
				html+='<td>'+result[i].deptName+'</td>';
				html+='<td>'+result[i].cardTime+'</td>';
				html+='<td title="'+result[i].reason+'">'+result[i].reason+'</td>';
				html+='<td>';
				html+='<button class="passBtn" onclick="pass(2,\''+result[i].id+'\',0);">通过</button>';
				html+='<button class="rejectBtn" onclick="rejectShow(2,\''+result[i].id+'\');">驳回</button>';
				html+='</td>';
				html+='</tr>';
			}
			if(result.length<=0){
				html+='<tr class="item-white">';
				html+='<td colspan="6"><div>暂无数据</div></td>';
				html+='</tr>';
				$("#page").html("");
			}else{
				var pageNo=data.data.current;
				var total_page=data.data.pages;
				localStorage.setItem("total_page",total_page);
				//分页
				$("#page").paging({
				    pageNo: parseInt(pageNo),
				    totalPage: parseInt(total_page),
				    callback: function(num) {
						$('#pageNo').val(num);
						var total_page=localStorage.getItem("total_page");
						$("#page").paging({
						    pageNo: parseInt(num),
						    totalPage: parseInt(total_page)
						})
						var paramData=getParam();
						getDataPatchcard(paramData);
						$("#page").stopPagination({});
				    }
				})
			}
			html+='</table>';
			$('#data_patchcard').html(html);
		}
	});
	
}
/* 给待审批（请假申请）页面赋值 */
function getDataLeave(param){
	
	var userToken=localStorage.getItem("userToken");
	var URL = site_url+"/leaves/page?size=10&current="+param.current+"&deptId="+param.deptId+"&start="+param.start+"&end="+param.end+"&keyword="+param.keyword+"&result=0";
	// var URL = site_url+"/leaves/approve";
	
	$.ajax({
		type:"get",
		url:URL,
		headers:{'token':userToken},
		success:function(data){
			var result = data.data.records;
			$('#data_leave').html("");
			var html="";
			html+='<table>';
			html+='<tr class="table-top"><td width="6%"><input id="allCheck" type="checkbox" value="0" onchange="selectAll()">';
			html+='</td><td width="7%">姓名</td><td width="8%">部门</td><td width="8%">请假类型</td><td width="15%">开始时间</td>';
			html+='<td width="15%">结束时间</td><td width="8%">请假时长</td><td width="18%">请假原因</td><td width="15%">操作</td>';
		
			for (var i=0;i<result.length;i++) {
				if(i%2==0){
					html+='<tr class="item-white" onclick="dataCheck(3,\''+result[i].id+'\',0);">';
				}else{
					html+='<tr class="item-blue" onclick="dataCheck(3,\''+result[i].id+'\',0);">';
				}
				html+='<td><input type="checkbox" name="dataId" value="'+result[i].id+'"></td>';
				html+='<td>'+result[i].name+'</td>';
				html+='<td>'+result[i].deptName+'</td>';
				html+='<td>'+result[i].label+'</td>';
				html+='<td>'+result[i].start+'</td>';
				html+='<td>'+result[i].end+'</td>';
				html+='<td>'+result[i].duration+'</td>';
				html+='<td>'+result[i].reason+'</td>';
				html+='<td>';
				html+='<button class="passBtn" onclick="pass(3,\''+result[i].id+'\',0);">通过</button>';
				html+='<button class="rejectBtn" onclick="rejectShow(3,\''+result[i].id+'\');">驳回</button>';
				html+='<button class="rejectBtn" onclick="dateDelete(1,\''+result[i].id+'\');">删除</button>';
				html+='</td>';
				html+='</tr>';
			}
			if(result.length<=0){
				html+='<tr class="item-white">';
				html+='<td colspan="9"><div>暂无数据</div></td>';
				html+='</tr>';
				$("#page").html("");
			}else{
				var pageNo=data.data.current;
				var total_page=data.data.pages;
				localStorage.setItem("total_page",total_page);
				//分页
				$("#page").paging({
				    pageNo: parseInt(pageNo),
				    totalPage: parseInt(total_page),
				    callback: function(num) {
						$('#pageNo').val(num);
						var total_page=localStorage.getItem("total_page");
						$("#page").paging({
						    pageNo: parseInt(num),
						    totalPage: parseInt(total_page)
						})
						var paramData=getParam();
						getDataLeave(paramData);
						$("#page").stopPagination({});
				    }
				})
			}
			html+='</table>';
			$('#data_leave').html(html);
			
		}
	});
}
/* -------------------------未审批数据赋值结束------------------------- */
/* -------------------------审批记录数据赋值开始------------------------- */
/* 给审批（加班申请）页面赋值 */
function getDataOvertimeApproval(param){
	
	var userToken=localStorage.getItem("userToken");
	var URL = site_url+"/overtimes/page?size=10&current="+param.current+"&deptId="+param.deptId+"&start="+param.start+"&end="+param.end+"&keyword="+param.keyword+"&result=1";
	
	$.ajax({
		type:"get",
		url:URL,
		headers:{'token':userToken},
		success:function(data){
			var result = data.data.records;

			var total_page=data.data.total/9;
			// alert(total_page);
			$('#total_page').val(data.data.pages);
			$('#total_rows').val(data.data.total);
			$('#data_overtime').html("");
			var html="";
			html+='<table>';
			html+='<tr class="table-top">';
			html+='<td width="7%">姓名</td><td width="7%">部门</td><td width="7%">加班类型</td><td width="14%">开始时间</td><td width="14%">结束时间</td>';
			html+='<td width="7%">加班时长</td><td width="14%">加班内容</td><td width="5%">状态</td><td width="10%">操作</td></tr>';
		
			for (var i=0;i<result.length;i++) {
				if(i%2==0){
					html+='<tr class="item-white">';
				}else{
					html+='<tr class="item-blue">';
				}
				var state = "";
				var statusNum=result[i].result;
				if(statusNum==2){
					state="驳回";
				}
				if(statusNum==11){
					state="审核中";
				}
				if(statusNum==1 || statusNum==12){
					state="通过";
				}
				html+='<td>'+result[i].name+'</td>';
				html+='<td>'+result[i].deptName+'</td>';
				var type=result[i].type==1?"调休":"有偿";
				html+='<td>'+type+'</td>';
				html+='<td>'+result[i].start+'</td>';
				html+='<td>'+result[i].end+'</td>';
				html+='<td>'+result[i].duration+'</td>';
				html+='<td>'+result[i].reason+'</td>';
				html+='<td>'+state+'</td>';
				html+='<td>';
				html+='<img id="check" style="margin-left: 10px;" src="img/check_icon.png" onclick="dataInfo(1,\''+result[i].id+'\');"/>';
				html+='</td>';
				html+='</tr>';
			}
			if(result.length<=0){
				html+='<tr class="item-white">';
				html+='<td colspan="9"><div>暂无数据</div></td>';
				html+='</tr>';
				$("#page").html("");
			}else{
				var pageNo=data.data.current;
				var total_page=data.data.pages;
				localStorage.setItem("total_page",total_page);
				//分页
				$("#page").paging({
				    pageNo: parseInt(pageNo),
				    totalPage: parseInt(total_page),
				    callback: function(num) {
						$('#pageNo').val(num);
						var total_page=localStorage.getItem("total_page");
						$("#page").paging({
						    pageNo: parseInt(num),
						    totalPage: parseInt(total_page)
						})
						var paramData=getParam();
						getDataOvertimeApproval(paramData);
						$("#page").stopPagination({});
				    }
				})
			}
			html+='</table>';
			$('#data_overtime').html(html);
			
		}
	});
}
/* 给审批（补卡说明）页面赋值 */
function getDataPatchcardApproval(param){
	
	var userToken=localStorage.getItem("userToken");
	var URL = site_url+"/replacecards/page?size=10&current="+param.current+"&deptId="+param.deptId+"&start="+param.start+"&end="+param.end+"&keyword="+param.keyword+"&result=1";
	
	$.ajax({
		type:"get",
		url:URL,
		headers:{'token':userToken},
		success:function(data){
			var result = data.data.records;
			
			$('#data_patchcard').html("");
			var html="";
			html+='<table>';
			html+='<tr class="table-top"><td width="15%">姓名</td><td width="15%">部门</td>';
			html+='<td width="20%">补卡时间</td><td width="25%">补卡原因</td><td width="10%">状态</td><td width="15%">操作</td>';
		
			for (var i=0;i<result.length;i++) {
				if(i%2==0){
					html+='<tr class="item-white">';
				}else{
					html+='<tr class="item-blue">';
				}
				var state = result[i].result==1?"审批通过":"驳回";
				html+='<td>'+result[i].name+'</td>';
				html+='<td>'+result[i].deptName+'</td>';
				html+='<td>'+result[i].cardTime+'</td>';
				html+='<td>'+result[i].reason+'</td>';
				html+='<td>'+state+'</td>';
				html+='<td>';
				html+='<img id="check" style="margin-left: 10px;" src="img/check_icon.png" onclick="dataInfo(2,\''+result[i].id+'\');"/>';
				html+='</td>';
				html+='</tr>';
			}
		
			if(result.length<=0){
				html+='<tr class="item-white">';
				html+='<td colspan="6"><div>暂无数据</div></td>';
				html+='</tr>';
				$("#page").html("");

			}else{
				var pageNo=data.data.current;
				var total_page=data.data.pages;
				localStorage.setItem("total_page",total_page);
				//分页
				$("#page").paging({
				    pageNo: parseInt(pageNo),
				    totalPage: parseInt(total_page),
				    callback: function(num) {
						$('#pageNo').val(num);
						var total_page=localStorage.getItem("total_page");
						$("#page").paging({
						    pageNo: parseInt(num),
						    totalPage: parseInt(total_page)
						})
						var paramData=getParam();
						getDataPatchcardApproval(paramData);
						$("#page").stopPagination({});
				    }
				})
			}
			html+='</table>';
			$('#data_patchcard').html(html);
			
		}
	});
}
/* 给审批（请假申请）页面赋值 */
function getDataLeaveApproval(param){
	$('#data_leave').html("");
	
	var URL = site_url+"/leaves/page?size=10&current="+param.current+"&deptId="+param.deptId+"&start="+param.start+"&end="+param.end+"&keyword="+param.keyword+"&result=1";
	var userToken=localStorage.getItem("userToken");
	
	$.ajax({
		type:"get",
		url:URL,
		headers:{'token':userToken},
		success:function(data){
			var result = data.data.records;
			
			var html="";
			html+='<table>';
			html+='<tr class="table-top"><td width="7%">姓名</td><td width="8%">部门</td><td width="8%">请假类型</td>';
			html+='<td width="15%">开始时间</td><td width="15%">结束时间</td><td width="8%">请假时长</td>';
			html+='<td width="18%">请假原因</td><td width="6%">状态</td><td width="15%">操作</td>';
		
			for (var i=0;i<result.length;i++) {
				if(i%2==0){
					html+='<tr class="item-white">';
				}else{
					html+='<tr class="item-blue">';
				}
				var state = result[i].result==1?"审批通过":"驳回";
				html+='<td>'+result[i].name+'</td>';
				html+='<td>'+result[i].deptName+'</td>';
				html+='<td>'+result[i].label+'</td>';
				html+='<td>'+result[i].start+'</td>';
				html+='<td>'+result[i].end+'</td>';
				html+='<td>'+result[i].duration+'</td>';
				html+='<td title="'+result[i].reason+'">'+result[i].reason+'</td>';
				html+='<td>'+state+'</td>';
				html+='<td>';
				html+='<img id="check" style="margin-left: 10px;" src="img/check_icon.png" onclick="dataInfo(3,\''+result[i].id+'\');"/>';
				html+='<img id="delete" style="margin-left: 10px;" src="img/delete_icon.png" onclick="dateDelete(1,\''+result[i].id+'\');"/>';
				html+='</td>';
				html+='</tr>';
			}
			if(result.length<=0){
				html+='<tr class="item-white">';
				html+='<td colspan="9"><div>暂无数据</div></td>';
				html+='</tr>';
				$("#page").html("");

			}else{
				var pageNo=data.data.current;
				var total_page=data.data.pages;
				localStorage.setItem("total_page",total_page);
				//分页
				$("#page").paging({
				    pageNo: parseInt(pageNo),
				    totalPage: parseInt(total_page),
				    callback: function(num) {
						$('#pageNo').val(num);
						var total_page=localStorage.getItem("total_page");
						$("#page").paging({
						    pageNo: parseInt(num),
						    totalPage: parseInt(total_page)
						})
						var paramData=getParam();
						getDataLeaveApproval(paramData);
						$("#page").stopPagination({});
				    }
				})
			}
			html+='</table>';
			$('#data_leave').html(html);
			
		}	
	});
}
/* 审批记录数据赋值结束 */
/* 查看功能 */
function dataInfo(type,id){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
		switch(type){
			/* 加班申请 */
			case 1:
				getOvertimeById(id);
				$("#infoDiv").show();
				break;
			/* 补卡说明 */
			case 2:
				getReplacecardsById(id);
				$("#infoDiv").show();
				break;
			/* 请假申请 */
			case 3:
				getLeaveById(id);
				$("#infoDiv").show();
				break;
			/* 考勤记录 */
			case 4:
				localStorage.setItem("attendanceUserId",id);
				var date=$('#search_month').val();
				localStorage.setItem("attendanceDate",date);
				location.href="check_work_attendance_info.html";
				break;
			default:
				break;
		}
	    // layer.alert("查看！"+id, {icon: 6});
	});	
}
/* 文件导出 */
function fileImport(){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
	    layer.alert("导出！", {icon: 6});
		// document.getElementById("importForm").submit();
	});	
}
/* 通过 */
function pass(type,id,status){
	/* 点击按钮时不出发点击一行的事件 */
	event.stopPropagation();
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
	    var URL = "";
	    var data={
			"overtimeId":id,
			"result":1,
			"reason":"正常"
		};
		var userToken=localStorage.getItem("userToken");
		switch(type){
			/* 加班申请 */
			case 1:
				if(id==""){
					id=$("#dataId").val();
					status=$("#status").val();
				}
				var data={
					"id":id,
					"result":status,
					"reason":"正常"
				};
				URL = site_url+"/overtimes";
				$.ajax({
					type:"GET",
					url:URL,
					headers:{'token':userToken},
					contentType: 'charset=utf-8',
					data:data,
					success:function(data){
						if (data.code == 0){
							layer.alert("审批成功！", {icon: 6});
						}
					}
				})
				break;
			/* 补卡说明 */
			case 2:
				if(id==''){
					id=$("#dataId").val();
				}
			    var data={
					"id":id,
					"result":1,
					"reason":"正常"
				};
				URL = site_url+"/replacecards";
				$.ajax({
					type:"GET",
					url:URL,
					headers:{'token':userToken},
					contentType: 'charset=utf-8',
					data:data,
					success:function(data){
						if (data.code == 0){
							layer.alert("审批成功！", {icon: 6});
						}
					}
				})
				break;
			/* 请假申请 */
			case 3:
				if(id==''){
					id=$("#dataId").val();
				}
				var data={
					"id":id,
					"result":1,
					"reason":"正常"
				};
				URL = site_url+"/leaves"
				$.ajax({
					type:"GET",
					url:URL,
					headers:{'token':userToken},
					contentType: 'charset=utf-8',
					data:data,
					success:function(data){
						if (data.code == 0){
							layer.alert("审批成功！", {icon: 6});
						}
					}
				})
				break;
			default:
				break;
		}
		location.reload();
		// document.getElementById("importForm").submit();
	});	
}
/* 加班申请驳回 id 0：通过详情驳回 */
function rejectShow(type,id){
	event.stopPropagation();
	if(id!=''){
		$("#dataId").val(id);
	}
	$("#rejectDiv").show();
}
/* 加班申请驳回 id 0：通过详情驳回 */
function reject(type,id){
	event.stopPropagation();
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
		
		var reason=$("#reject_reason").val();
		if(reason==""){
			layer.alert("驳回原因不能为空！", {icon: 6});
			return;
		}
		
		var URL = "";
		var data={
			"id":id==''?$("#dataId").val():id,
			"result":2,
			"reason":$("#reject_reason").val()
		};
		var userToken=localStorage.getItem("userToken");
		switch(type){
			/* 加班申请 */
			case 1:
				URL = site_url+"/overtimes?id="+data.id+"&result=2&reason="+data.reason;
				break;
			/* 补卡说明 */
			case 2:
				URL = site_url+"/replacecards?id="+data.id+"&result=2&reason="+data.reason;
				break;
			/* 请假申请 */
			case 3:
				URL = site_url+"/leaves?id="+data.id+"&result=2&reason="+data.reason
				break;
			default:
				break;
		}
		
		$.ajax({
			type:"GET",
			url:URL,
			headers:{'token':userToken},
			contentType: 'application/json; charset=utf-8',
			// data:JSON.stringify(data),
			success:function(data){
				if (data.code == 0){
					layer.alert("驳回成功！", {icon: 6});
					location.reload();
				}
			}
		}) 
	});	
}
/* 查看详情功能的关闭按钮 */
function Close(){
	$("#infoDiv").hide();
}
/* 驳回功能的关闭按钮 */
function rejectClose(){
	$("#reject_reason").val("");
	$("#rejectDiv").hide();
}
/* 编辑详情功能的关闭按钮 */
function editClose(){
	$("#editDiv").hide();
}

/* 添加页面 */
function add(){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
		$("#addDiv").show();
	});
}
/* 添加的关闭按钮 */
function addClose(){
	$("#addDiv").hide();
}
/* 数据详情查看 */
function dataCheck(type,id,status){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
		switch(type){
			/* 加班申请 */
			case 1:
				$("#status").val(status);
				getOvertimeById(id);
				break;
			/* 补卡说明 */
			case 2:
				getReplacecardsById(id);
				break;
			/* 请假申请 */
			case 3:
				getLeaveById(id);
				break;
			default:
				break;
		}
		$("#infoDiv").show();
		// document.getElementById("importForm").submit();
	});	
}
/* 搜索功能 */
function search(id){
		$('#pageNo').val("1");
		var deptId=$("#search_department option:selected").val(); 
    
	    var start=$('#search_date_start').val();
	    
	    var end=$('#search_date_end').val();
	    
	    var keyword=$('#search_keyword').val();
	    var userInfo=JSON.parse(localStorage.getItem("userInfo"));
	    var userDepartId=userInfo.deptId; 
	 	var param = {
	    	current:$('#pageNo').val(),
	     	deptId:deptId==undefined?userDepartId:deptId,
	     	start:start,
	     	end:end,
	     	keyword:keyword
	 	};
	 	
	 	switch(id){
		//待审批页面(加班申请)
		case 311:
			getDataOvertime(param);
			break;	
		//待审批页面(补卡说明)
		case 312:
			getDataPatchcard(param);
			break;
		//待审批页面(请假申请)
		case 313:
			getDataLeave(param);
		//审批记录页面(加班申请)
		case 331:
			getDataOvertimeApproval(param);
			break;	
		//审批记录页面(补卡说明)
		case 332:
			getDataPatchcardApproval(param);
			break;
		//审批记录页面(请假申请)
		case 333:
			getDataLeaveApproval(param);	   
			break;
		//考勤记录页面
		case 32:
			var search_month=$('#search_month').val();
			var param = {
				current:$('#pageNo').val(),
			 	deptId:deptId==(undefined || "")?'-1':deptId,
			 	start:search_month,
			 	end:"",
			 	keyword:keyword
			};
			getDataRecord(param);	   
			break;
		default:
			break;
	}	

}
/* 一键通过按钮 */
function allPass(type){
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
		var chk_value = [];
        $('input[name="dataId"]:checked').each(function () {
            chk_value.push($(this).val());
			   this.checked = "checked"; 
        });
		
		var userToken = localStorage.getItem("userToken");
		
		switch (type){
			case 311:
				$.ajax({
					type:"get",
					url:site_url+"/overtimes/list/"+chk_value,
					headers:{'token':userToken},
					success:function(data){
						layer.alert("一键通过！", {icon: 6});
					}
				})
				break;
			case 312:
				$.ajax({
					type:"get",
					url:site_url+"/replacecards/list/"+chk_value,
					headers:{'token':userToken},
					success:function(data){
						layer.alert("一键通过！", {icon: 6});
					}
				})
				break;
			case 313:
				$.ajax({
					type:"get",
					url:site_url+"/leaves/list/"+chk_value,
					headers:{'token':userToken},
					success:function(data){
						layer.alert("一键通过！", {icon: 6});
					}
				})
			
				break;
			default:
				break;
		}
		location.reload();
	});	
}
/* 全选按钮 */
function selectAll(){
	// alert("ddd");
	var isCheck=$("#allCheck").is(':checked');  //获得全选复选框是否选中
	// alert(isCheck);
	$("input[type='checkbox']").each(function() {  
	    this.checked = isCheck;       //循环赋值给每个复选框是否选中
	});   
	// $('input[name="dataId"]:checked').prop("checked", true);
}
/* 待审批加班详情 */
function getOvertimeById(id){
	
	var URL = site_url+"/overtimes/detail?overtimeRecordId="+id
	var userToken=localStorage.getItem("userToken");
	$.ajax({
		type:"get",
		url:URL,
		headers:{'token':userToken},
		success:function(data){
			if (data.code==0){
				$("#dataId").val(data.data.id);
				$("#username").html(data.data.name);  
				$("#department").html(data.data.deptName);  
				$("#start_date").html(data.data.start);  
				$("#end_date").html(data.data.end);  
				$("#overtime_hours").html(data.data.duration); 
				var type=data.data.type;
				if(type==1){
					$("#overtime_type").html("调休");
				}else{
					$("#overtime_type").html("有偿");
				}
				 
				$("#overtime_content").val(data.data.reason);  
			}
		}
	});
}
/* 待审批补卡详情 */
function getReplacecardsById(id){
	
	var URL = site_url+"/replacecards/detail?replaceCardRecordId="+id
	var userToken=localStorage.getItem("userToken");
	$.ajax({
		type:"get",
		url:URL,
		headers:{'token':userToken},
		success:function(data){
			if (data.code==0){
				$("#dataId").val(data.data.id);
				$("#username").html(data.data.name);  
				$("#department").html(data.data.deptName);  
				$("#start_date").html(data.data.cardTime);  
				$("#patchcard_reason").html(data.data.reason);  
			}
		}
	});
}

/* 请假申请详情 */
function getLeaveById(id){
	
	var URL = site_url+"/leaves/detail?leaveRecordId="+id
	var userToken=localStorage.getItem("userToken");
	$.ajax({
		type:"get",
		url:URL,
		headers:{'token':userToken},
		success:function(data){
			if (data.code==0){
				$("#dataId").val(data.data.id);
				$("#username").html(data.data.name);  
				$("#department").html(data.data.deptName);  
				$("#start_date").html(data.data.start);  
				$("#end_date").html(data.data.end);  
				$("#leave_hours").html(data.data.duration); 
				$("#leave_type").html(data.data.label);
				$("#leave_reason").html(data.data.reason);  
			}
		}
	});
}

/* 获取用户信息 */
function getUserInfo(){

		var userToken=localStorage.getItem("userToken");
		var URL=site_url+"/login/user";
		var user="";
		$.ajax({
		    type: 'GET',
		    url: URL,
		    dataType:"json",
			headers:{'token':userToken},
			async:false,
		    success: function (json) {
				user=json.data;
		    }
		});
		return user;
}

function getDeptById(id){
	$("#search_project").val();
	$("#search_department").val();
	var result=[];
	var userToken = localStorage.getItem('userToken');
	$.ajax({
		url:site_url+"/depts/children?id="+id,
		dataType:'json',
		type:'GET',
		async:false,
		headers:{'token':userToken},
		success:function(data){
			result = data.data
		}
	})
	return result;
}
/* 删除操作 */
function dateDelete(type,id){
	event.stopPropagation();
	layui.use(['element', 'layer'], function() {
	    var layer = layui.layer;
	    
	    var userToken=localStorage.getItem("userToken");
	    var data={
			id:id
		};
		switch(type){
			//审批记录和待审批请假删除
			case 1:
				var URL=site_url+"/leaves?id="+id;
				// alert(id);
				layer.confirm('确定删除该数据吗？', { icon: 3, title: '提示',shade:0 }, function (index) {
					$.ajax({
						type: 'DELETE',
						url: URL,
						dataType:"json",
						headers:{'token':userToken},
						success: function (json) {
							var res=json.code;
							if(res==0){
								layer.alert("删除成功！", {icon: 6,shade:0});
								location.reload();
							}else if(res==1){
								layer.alert(json.msg, {icon: 5,shade:0});
							}
						}
					});
					layer.close(index);
				});
		
				break;	
			default:
				break;
		}	
	});
}